# build pxe loaders (from pxe + pbs) and disk loaders (disk + usb)
objtype=386
</$objtype/mkfile

TARG=\
	9boot\
	9bootpbs\
	9load\
	9loadusb\

BIN=/386

PXEBASE=0x80007c00
PBSBASE=0x80010000
# assume we've been decompressed into place by our header.
# BOOTBASE must match expand.c's Kerneladdr|KZERO; +0x20 allows for a.out header
BOOTBASE=0x80900020
EXTRACOPIES=

SFX=''				# defaults
BASE=boot

ONPROC=$NPROC
NPROC=1				# don't build all boot flavours at once

%.$O:	%.s
	$AS $AFLAGS $stem.s

%.$O:	%.c
	$CC $CFLAGS $stem.c

all:V: $TARG

install:V: 9boot.install 9bootpbs.install 9load.install 9loadusb.install

pcclean pclean:V:
	@ { cd ../pc && mk clean }
clean:V:
	rm -f 8.* bootmain bootmain.c
	@ {
		rfork ne; bind bootmkfile mkfile
		CONF=bootpbs SFX=pbs mk clean
		BASE=load CONF=loadusb mk clean
		BASE=load CONF=loadusb SFX=pxe mk clean
		for (BASE in boot load)
			CONF=$BASE mk clean
	}

%.calls:
	@ { rfork ne; bind bootmkfile mkfile
	KTZERO=$PXEBASE CONF=boot mk $target }

#
# pxe-loaded pxe loader
#
9bootmain 9bootmaindebug:V:
	cp boot bootmain
	@ { rfork ne; bind bootmkfile mkfile
	KTZERO=$BOOTBASE CONF=bootmain NPROC=$ONPROC
	mk all }

# creating $O.expand also creates $O.expanddebug, out of mk's sight
9boot:D: $O.expand 9bootmaindebug
	cp 9bootmaindebug 9bootdebug		# for 9boot.install
	{ cat $O.expand; strip -o /fd/1 9bootmaindebug | gzip -9 } >$target

# creating 9boot also creates 9bootdebug, out of mk's sight
9boot.install:V: 9boot
	cp 9boot 9bootdebug /$objtype/ &
	for(i in $EXTRACOPIES)
		{ 9fs $i && cp $prereq /n/$i/$objtype && echo -n $i... & }
	wait
	echo

#
# disk-resident pxe loader, loaded by pbs
#
9bootpbs:V:
	@ { rfork ne; bind bootmkfile mkfile
	KTZERO=$PBSBASE CONF=bootpbs SFX=pbs
	NPROC=$ONPROC START=l16r.$O; mk all }

9bootpbs.install:V: 9bootpbs
	@ { rfork ne; bind bootmkfile mkfile
	KTZERO=$PBSBASE CONF=bootpbs SFX=pbs
	NPROC=$ONPROC START=l16r.$O; mk install }

#
# disk-resident disk loaders, loaded by pbs
#
9load:V:
	@ { rfork ne; bind bootmkfile mkfile
	KTZERO=$PBSBASE BASE=load CONF=load
	NPROC=$ONPROC START=l16r.$O; mk all }

9load.install:V: 9load
	@ { rfork ne; bind bootmkfile mkfile
	KTZERO=$PBSBASE BASE=load CONF=load
	NPROC=$ONPROC START=l16r.$O; mk install }

9loadusb:V:
	@ { rfork ne; bind bootmkfile mkfile
	KTZERO=$PBSBASE BASE=load CONF=loadusb
	NPROC=$ONPROC START=l16r.$O; mk all }

9loadusb.install:V: 9loadusb
	@ { rfork ne; bind bootmkfile mkfile
	KTZERO=$PBSBASE BASE=load CONF=loadusb
	NPROC=$ONPROC START=l16r.$O; mk install }

#
# pxe-loaded disk loaders for convenient debugging only
#
9loadpxe:V:
	@ { rfork ne; bind bootmkfile mkfile
	KTZERO=$PXEBASE BASE=load CONF=loadpxe SFX=pxe
	NPROC=$ONPROC START=l16r.$O; mk all }

9loadpxe.install:V: 9loadpxe
	@ { rfork ne; bind bootmkfile mkfile
	KTZERO=$PXEBASE BASE=load CONF=loadpxe SFX=pxe
	NPROC=$ONPROC START=l16r.$O; mk install }

9loadusbpxe:V:
	@ { rfork ne; bind bootmkfile mkfile
	KTZERO=$PXEBASE BASE=load CONF=loadusbpxe SFX=pxe
	NPROC=$ONPROC START=l16r.$O; mk all }

9loadusbpxe.install:V: 9loadusbpxe
	@ { rfork ne; bind bootmkfile mkfile
	KTZERO=$PXEBASE BASE=load CONF=loadusbpxe SFX=pxe
	NPROC=$ONPROC START=l16r.$O; mk install }

#
# 8.expand: the decompressing header for 9boot
#
# for pbs, 0x10000 (64K), for pxe, 0x7c00 (31K)
LOADADDR=0x7c00

cga.tiny.$O expand.$O: expand.h inflate.guts.c

$O.expand: ldecomp.$O cga.tiny.$O expand.$O
	$LD -o $target^debug -R1 -T$LOADADDR $prereq
	$LD -o $target   -H3 -R1 -T$LOADADDR $prereq
